package structs

import (
	"fmt"
	"parking/src/common"
	"parking/src/enum"
	"strconv"
	"time"
)

// MQTTMessageStruct MQTT消息结构体
type MQTTMessageStruct struct {
	Topic    string
	Qos      byte
	Retained bool
	Payload  []byte
}

func NewMQTTMessage(topic string, qos byte, retained bool, payload []byte) *MQTTMessageStruct {
	return &MQTTMessageStruct{
		Topic:    topic,
		Qos:      qos,
		Retained: retained,
		Payload:  payload,
	}
}

// OSSUploadPictureStruct OSS图片上传结构体
type OSSUploadPictureStruct struct {
	BucketName string
	ObjectName string
	PicData    []byte
}

func NewOSSUploadPictureStruct(bucketName, objectName string, picData []byte) *OSSUploadPictureStruct {
	return &OSSUploadPictureStruct{
		BucketName: bucketName,
		ObjectName: objectName,
		PicData:    picData,
	}
}

// LocalStorePictureStruct 图片本地存储结构体
type LocalStorePictureStruct struct {
	FullPath string
	PicData  []byte
}

func NewLocalStorePictureStruct(fullPath string, picData []byte) *LocalStorePictureStruct {
	return &LocalStorePictureStruct{
		FullPath: fullPath,
		PicData:  picData,
	}
}

type AccessServerStruct struct {
	Cmd       string `json:"cmd"`
	MsgId     string `json:"msgId"`
	CtrlDevId string `json:"ctrlDevId"`
	UtcTs     string `json:"utcTs"`
}

// NewAccessServerStruct 是一个构造函数，用于创建 AccessServerStruct 的实例
func NewAccessServerStruct(cmd, ctrlDevId string) *AccessServerStruct {
	utcTs := time.Now().UnixMilli()
	msgId := common.GenerateMsgId()
	return &AccessServerStruct{
		Cmd:       cmd,
		CtrlDevId: ctrlDevId,
		MsgId:     msgId,
		UtcTs:     strconv.FormatInt(utcTs, 10),
	}
}

type HearBeatStruct struct {
	Cmd   string `json:"cmd"`
	MsgId string `json:"msgId"`
	DevId string `json:"devId"`
	DevIp string `json:"devIp"`
	UtcTs string `json:"utcTs"`
}

func NewHearBeatStruct(devId string, devIp string) *HearBeatStruct {
	utcTs := time.Now().UnixMilli()
	msgId := common.GenerateMsgId()
	return &HearBeatStruct{
		Cmd:   "heartbeat",
		DevId: devId,
		DevIp: devIp,
		MsgId: msgId,
		UtcTs: strconv.FormatInt(utcTs, 10),
	}
}

type PlateResultStruct struct {
	Cmd       string                     `json:"cmd"`   // 固定为 "plateResult"
	MsgId     string                     `json:"msgId"` // 消息 ID
	CtrlDevId string                     `json:"ctrlDevId"`
	DevId     string                     `json:"devId"`   // 相机 ID
	ParkId    string                     `json:"parkId"`  // 停车场 ID
	DevIp     string                     `json:"devIp"`   // 相机 IP 地址，可选
	UtcTs     int64                      `json:"utcTs"`   // UTC 时间戳
	Content   *LicencePlateMessageStruct `json:"content"` // 车牌结果 json 数据
}

func NewPlateResultStruct(ip string, devId string, licencePlateMessageStruct *LicencePlateMessageStruct) *PlateResultStruct {
	return &PlateResultStruct{
		Cmd:       "plateResult",
		MsgId:     common.GenerateMsgId(),
		UtcTs:     time.Now().UnixMilli(),
		CtrlDevId: "b06835cbdfd714a9",
		DevId:     devId,
		DevIp:     ip,
		Content:   licencePlateMessageStruct,
	}
}

// LicencePlateMessageStruct 车牌信息结构体
type LicencePlateMessageStruct struct {
	PlateNum     string `json:"plateNum"`     // 车牌号
	PlateColor   string `json:"plateColor"`   // 车牌底色
	PlateVal     bool   `json:"plateVal"`     // 是否虚假车牌
	Confidence   int    `json:"confidence"`   // 车牌置信度，可选
	CarLogo      string `json:"carLogo"`      // 车辆品牌，可选
	CarColor     string `json:"carColor"`     // 车辆颜色，可选
	VehicleType  string `json:"vehicleType"`  // 车辆类型，可选
	TriggerTime  int    `json:"triggerTime"`  // 车牌识别触发时间
	InOut        string `json:"inOut"`        // 出入口类型
	IsWhitelist  bool   `json:"isWhitelist"`  // 是否白名单车辆
	TriggerType  string `json:"triggerType"`  // 触发方式
	FullPicPath  string `json:"fullPicPath"`  // 全景图上传路径，可选
	PlatePicPath string `json:"platePicPath"` // 特写图上传路径，可选
}

func NewLicencePlateMessageStruct() *LicencePlateMessageStruct {
	return &LicencePlateMessageStruct{}
}
func (l *LicencePlateMessageStruct) BuildLicencePlateMessageStruct(p *PacketBody, fileName string) {
	l.SetPlateNum(p.GetPlateInfo().GetLicensePlate())
	l.SetPlateColor(enum.GetPlateColor(p.GetPlateInfo().GetPlateColor()))
	l.SetPlateVal(p.GetFalsePlate() != 0)
	l.SetConfidence(p.GetPlateInfo().GetConfidence())
	l.SetTriggerTime(DateTimeToStamp(p.GetLiveCapTime()))
	l.SetTriggerType(enum.GetTriggerType(p.GetAlarmType()))
	l.SetFullPicPath(fileName)
	lStr := fmt.Sprintf("车牌号: %s, 车牌颜色: %s, 是否真实车牌: %t, Confidence: %d, 触发时间: %d, 触发类型: %s",
		l.GetPlateNum(), l.GetPlateColor(), l.GetPlateVal(), l.GetConfidence(), l.GetTriggerTime(), l.GetTriggerType())
	logger.Infof("车牌信息: %s", lStr)
}

//func NewLicencePlateMessageStruct(plateNum string, plateColor string, plateVal bool, confidence int, carLogo string, carColor string, vehicleType string, triggerTime int64, inOut string, isWhitelist bool, triggerType string, fullPicPath string, platePicPath string) *LicencePlateMessageStruct {
//	return &LicencePlateMessageStruct{
//		PlateNum:     plateNum,
//		PlateColor:   plateColor,
//		PlateVal:     plateVal,
//		Confidence:   confidence,
//		CarLogo:      carLogo,
//		CarColor:     carColor,
//		VehicleType:  vehicleType,
//		TriggerTime:  triggerTime,
//		InOut:        inOut,
//		IsWhitelist:  isWhitelist,
//		TriggerType:  triggerType,
//		FullPicPath:  fullPicPath,
//		PlatePicPath: platePicPath,
//	}
//}

func (m *MQTTMessageStruct) GetTopic() string {
	return m.Topic
}

func (m *MQTTMessageStruct) GetQos() byte {
	return m.Qos
}

func (m *MQTTMessageStruct) GetRetained() bool {
	return m.Retained
}

func (m *MQTTMessageStruct) GetPayload() []byte {
	return m.Payload
}

//  OSSUploadPictureStruct OSS图片上传结构体的 Get 函数

func (o *OSSUploadPictureStruct) GetBucketName() string {
	return o.BucketName
}

func (o *OSSUploadPictureStruct) GetObjectName() string {
	return o.ObjectName
}

func (o *OSSUploadPictureStruct) GetPicData() []byte {
	return o.PicData
}

// LocalStorePictureStruct 图片本地存储结构体的 Get 函数

func (l *LocalStorePictureStruct) GetAbsolutePath() string {
	return l.FullPath
}

func (l *LocalStorePictureStruct) GetPicData() []byte {
	return l.PicData
}

func (l *LicencePlateMessageStruct) SetPlateNum(plateNum string) {
	l.PlateNum = plateNum
}

func (l *LicencePlateMessageStruct) GetPlateNum() string {
	return l.PlateNum
}

func (l *LicencePlateMessageStruct) SetPlateColor(plateColor string) {
	l.PlateColor = plateColor
}

func (l *LicencePlateMessageStruct) GetPlateColor() string {
	return l.PlateColor
}

func (l *LicencePlateMessageStruct) SetPlateVal(plateVal bool) {
	l.PlateVal = plateVal
}

func (l *LicencePlateMessageStruct) GetPlateVal() bool {
	return l.PlateVal
}

func (l *LicencePlateMessageStruct) SetConfidence(confidence int) {
	l.Confidence = confidence
}

func (l *LicencePlateMessageStruct) GetConfidence() int {
	return l.Confidence
}

func (l *LicencePlateMessageStruct) SetCarLogo(carLogo string) {
	l.CarLogo = carLogo
}

func (l *LicencePlateMessageStruct) GetCarLogo() string {
	return l.CarLogo
}

func (l *LicencePlateMessageStruct) SetCarColor(carColor string) {
	l.CarColor = carColor
}

func (l *LicencePlateMessageStruct) GetCarColor() string {
	return l.CarColor
}

func (l *LicencePlateMessageStruct) SetVehicleType(vehicleType string) {
	l.VehicleType = vehicleType
}

func (l *LicencePlateMessageStruct) GetVehicleType() string {
	return l.VehicleType
}

func (l *LicencePlateMessageStruct) SetTriggerTime(triggerTime int) {
	l.TriggerTime = triggerTime
}

func (l *LicencePlateMessageStruct) GetTriggerTime() int {
	return l.TriggerTime
}

func (l *LicencePlateMessageStruct) SetInOut(inOut string) {
	l.InOut = inOut
}

func (l *LicencePlateMessageStruct) GetInOut() string {
	return l.InOut
}

func (l *LicencePlateMessageStruct) SetIsWhitelist(isWhitelist bool) {
	l.IsWhitelist = isWhitelist
}

func (l *LicencePlateMessageStruct) GetIsWhitelist() bool {
	return l.IsWhitelist
}

func (l *LicencePlateMessageStruct) SetTriggerType(triggerType string) {
	l.TriggerType = triggerType
}

func (l *LicencePlateMessageStruct) GetTriggerType() string {
	return l.TriggerType
}

func (l *LicencePlateMessageStruct) SetFullPicPath(fullPicPath string) {
	l.FullPicPath = fullPicPath
}

func (l *LicencePlateMessageStruct) GetFullPicPath() string {
	return l.FullPicPath
}

func (l *LicencePlateMessageStruct) SetPlatePicPath(platePicPath string) {
	l.PlatePicPath = platePicPath
}

func (l *LicencePlateMessageStruct) GetPlatePicPath() string {
	return l.PlatePicPath
}
